<?php
/**
 *   Joomdiet - components/com_joomdiet/models/diet.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2011 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');
error_reporting(E_ALL); 
ini_set("display_errors", 1);
class JoomdietControllerDiet extends JController
{
    function __construct()
    {
        if (JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        
        $this->item_type = 'Default';
        
        parent::__construct();
    }
    
    function show_document()
    {
        $params =& JComponentHelper::getParams('com_joomdiet');
        $view_mode = $params->get('document_handling');
        
        if ($view_mode == 0) {
            $view =& $this->getView('docdownload', 'html');
        } else $view =& $this->getView('docgoogle', 'html');
        
        $view->setModel($this->getModel('diet'), true);
        
        $view->display();
    }
    
    function download() {
        $model = $this->getModel('diet');
        
        $model->setId(JRequest::getVar('diet_id'));
        
        if ($file = $model->getDiet()) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.$file->file_name);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: '.$file->file_size);
            
            ob_clean();
            flush();
            readfile($file->file_url);
            exit;
        }
    }
    
    function download_thumb() {
        $model = $this->getModel('diet');
        
        $model->setId(JRequest::getVar('diet_id'));
        
        if ($file = $model->getDiet()) {
            header('Content-Description: File Transfer');
            header('Content-Type: image/jpeg');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: '.filesize(COM_JOOMDIET_UPLOAD_DIR.DS.'thumbs'.DS.JFile::stripExt($this->diet->file_name)));
            
            ob_clean();
            flush();
            readfile(COM_JOOMDIET_UPLOAD_DIR.DS.'thumbs'.DS.JFile::stripExt($this->diet->file_name).'.jpg');
            exit;
        }
    }
}
?>
